package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ContractInfo;
import ywpt.model.ContractType;
import ywpt.service.config.ContractTypeService;
import ywpt.service.contract.ContractService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ContractTypeAction extends BaseAction implements
ModelDriven<ContractType>, Preparable {

	/**
	 * 合同类型管理Action
	 */
	private static final long serialVersionUID = -7261851284318335235L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private	Long	id;
	private Long 	classId;
	private String	typeCode;
	private	String	typeName;
	private String	remark;
	
	private List<Long>  	groupList = new ArrayList<Long>();
	private	ContractType contractType;
	private ContractTypeService	contractTypeService;
	private	List<ContractType> contractTypeList = new ArrayList<ContractType>();
	
	private ContractService contractService;
	
	
	
	
	
	public ContractService getContractService() {
		return contractService;
	}
	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getTypeCode() {
		return typeCode;
	}
	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}
	public String getTypeName() {
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public ContractType getContractType() {
		return contractType;
	}
	public void setContractType(ContractType contractType) {
		this.contractType = contractType;
	}
	public ContractTypeService getContractTypeService() {
		return contractTypeService;
	}
	public void setContractTypeService(ContractTypeService contractTypeService) {
		this.contractTypeService = contractTypeService;
	}
	public List<ContractType> getContractTypeList() {
		return contractTypeList;
	}
	public void setContractTypeList(List<ContractType> contractTypeList) {
		this.contractTypeList = contractTypeList;
	}
	public Long getClassId() {
		return classId;
	}
	public void setClassId(Long classId) {
		this.classId = classId;
	}
	public List<Long> getGroupList() {
		return groupList;
	}
	public void setGroupList(List<Long> groupList) {
		this.groupList = groupList;
	}
	// ****************************默认方法*******************************
	@Override
	public ContractType getModel() {
		return this.contractType;
	}
	@Override
	@SuppressWarnings("unchecked")
	public String execute() {
		return null;
	}
	@Override
	public void prepare() throws Exception {
		if (id != null) {
			this.contractType = contractTypeService.getContractTypeById(id);
		} else {
			this.contractType = new ContractType();
		}
	}
	// **************************数据交互方法*******************************

	public String group(){
			groupList.addAll(contractTypeService.getClassGroup());
		return SUCCESS;
	}
	
	public String list(){
		DetachedCriteria query=DetachedCriteria.forClass(ContractType.class);
		if(classId != null){
			query.add(Restrictions.eq("classId", classId));			
		}
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows=contractTypeService.getContractTypeCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),
				totalRows,DEFAULT_PAGE_SIZE,0);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("typeCode"));
		query.setProjection(null);
		contractTypeList.addAll(contractTypeService.getContractTypes(query,pageSize,
				rowStartEnd[0]));
		return SUCCESS;
	}
	
	public String input(){	
		String typeCode = this.contractTypeService.CreateTypeCode( );
		contractType.setTypeCode(typeCode);
		return INPUT;
	} 

	public String edit(){
		this.contractType = contractTypeService.getContractTypeById(id);
		return INPUT;
	}
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		System.out.println("进入save");
		System.out.println(classId);
		System.out.println(contractType.getClassId());
		try {
			if (id == null) {
				contractTypeService.saveContractType(contractType);
			} else {
				contractTypeService.updateContractType(contractType);
			}
			this.addActionMessage("类型信息已保存!");
			return SUCCESS;
		} catch (UniqueConstraintException e) {
			if (e.getMessage().indexOf("already exists") != -1) {
				this.addFieldError("typeCode", "编码重复");
			} else {
				this.addActionError("保存错误:" + e.getMessage());
			}
			return INPUT;
		}
	}
	 public String delete() throws Exception {
			if (id != null) {
				DetachedCriteria congtractQuery = DetachedCriteria.forClass(ContractInfo.class);
				congtractQuery.add(Restrictions.eq("depart", contractTypeService.getContractTypeById(id)));
				int totalRows = contractService.getContractInfoCountByCriteria(congtractQuery);
				if (totalRows > 0) {
					this.addActionMessage("该客户已经发生过业务逻辑，不能删除！");
					return INPUT;
				}else{
					contractTypeService.removeContractType(id);
					return SUCCESS;					
				}

			}
			this.addActionMessage("部门编号为空，请选择具体的记录！");
			return INPUT;
	}

	 public String select() {
		 DetachedCriteria query= DetachedCriteria.forClass(ContractType.class);
		 query.add(Restrictions.eq("classId",Long.valueOf("1")));
		 contractTypeList.addAll(contractTypeService.getContractTypes(query));
		return SUCCESS;
	}

}
